

/******************************************************************************
* Copyright (C) 2021, BBI Technology Co.,Ltd All rights reserved.
*
* Author: Embedded software group
*
* file： api_valvemotor.c 
*
*
*Note：此文件位于对象层
 ******************************************************************************/
 
#include "api_nbiot.h"

QueueHandle_t  nbiot_Queue_data_tx = NULL; //用于存放在
QueueHandle_t  nbiot_Queue_info = NULL;
SemaphoreHandle_t    F_RspWaiting_BinarySemaphore=NULL;	//等到接收完成二值信号量句柄


nbiot_info_Struct  nbiot_info;
nbiot_Struct  nbiot;

char *AT_NBIoTCmdTbl[]={
    "ATI",                  //NB版本号
    "+NCONFIG",             //扰码开关配置
    "+CFUN",                //射频开关
    "+NCSEARFCN",           //清除频点
    "+NBAND",               //工作频段
    "+NRB",                 //软重启
    "+CMEE",                //错误提示
    #if (NBIoT_IMEI_PARAM_USED>0u)
    "+CGSN",                //查询IMEI号
    "+NTSETID",             //设置IMEI号
    #endif
    #if (NBIoT_CIMI_PARAM_USED> 0u)
    "+CIMI",                //查询 SIM 卡信息
    #endif
    #if (NBIoT_CCID_PARAM_USED>0u)
    "+NCCID",               //查询CCID
    #endif
    "+NCDP",                //配置IP地址
    "+CGDCONT",             //APN配置
    "+CSCON",               //信号连接状态
    "+NNMI",                //开启下行数据通知
    "+CGATT",               //自动搜网
    "+CSQ",                 //信号强度
    "+NUESTATS",            //查询 UE 状态
    "+CGPADDR",             //查询PDP地址
    "+CCLK",                //查询网络时间
    "+CEREG",               //连接核心网通知
    "+NSOCR",               //建立 socket
    "+NSOCL",               //关闭 socket
    "+NSONMI",              //接收数据提示
    "+NSORF",               //读取接收数据      
    #if (NBIOT_TRANS_PROTOCOL_SEL==NBIOT_TRANS_PROTOCOL_UDP)
    "+NSOST",               //udp发送数据 
    "+NSOSTF",              //udp发送数据带标志
    #elif (NBIOT_TRANS_PROTOCOL_SEL==NBIOT_TRANS_PROTOCOL_TCP)
    "+NSOSD",               //tcp发送数据
    "+NSOCO",               //tcp建立连接/
    #else
    "+NMSTATUS",            //查询COAP平台注册状态
    "+NMGS",                //coap发送数据 
    "+NMGR",                //coap查询数据
    #endif
    "+CPSMS",               //PSM模式配置
    "+CEDRXS",				      //eDRX Setting
    "+NPTWEDRXS",
    "+NPSMR",                //查询PSM状态
    "+QIOPEN",               //BC26建立一个 套接字并且连接平台
    "+QRST",                 //软重启
    "+QCCID",                //BC26 获取卡的ID 
    "+QBAND",                //BC26设置工作频段
     "ATE1",                 //关闭回显
    "+QISENDEX",             //BC26 发送
    "+QIRD",                 //BC26读取接收数据
    "+QICFG",
    "+COPS",                 //BC26设置 基站附着选择
    "+IPR",                  //BC26设置波特率
    "AT&W",                  //BC26保存 波特率设置，复位后保存
    "+QICLOSE",
	"+QLACFG" ,               //配置目标平台为电信 IoT 平台
	"+QLACONFIG",             //配置注册参数
	"+QLAADDOBJ",             //添加对象
	"+QLAREG",                //注册平台
	"+QLAUPDATE",             //发送数据请求
	"+QLANOTIFY",             //上报呗订阅的数据
	"+QLAOBSRSP" ,            //响应订阅请求      
	"+QLASENDDATA",           //直接发送数据		
	"+CBC"        ,           //260Y 查询 模块得电压只
	"+QLAWRRSP" ,             //260Y 回复平台得写操作指令
 };





uint8_t  API_Nbiot_AT_COM_Encode(uint8_t CmdIndex,uint8_t CmdType,uint8_t *pCmdParam,uint16_t CmdParamLen,uint16_t RepeatTime, uint16_t TimeoutTicks)
{
    uint8_t TxBuff[500];
    uint16_t TxLen = 0;
	uint16_t i = 0;
    uint8_t *pStr = (uint8_t *)AT_NBIoTCmdTbl[CmdIndex];//根据查表法，查询指令
   

    if( (CmdIndex != NB_CMD_ATI) && (CmdIndex != NB_CMD_ATE0) )
        TxLen += MemCopy(TxBuff + TxLen, "AT", 2, 0);
    
    //如果当前指令索引不为"AT"
    if (CmdIndex != NB_CMD_NULL)
        TxLen += MemCopy(TxBuff + TxLen, pStr, 0, 0);
    
    //如果当前指令类型为查询指令
    if (CmdType == NB_CMD_TYPE_QUERY)
    {
        TxLen += MemCopy(TxBuff + TxLen, "?", 1, 0);
    }
    //如果当前指令为测试指令
    else if (CmdType == NB_CMD_TYPE_TEST)
    {
        TxLen += MemCopy(TxBuff + TxLen, "=?", 2, 0);
    }
    //如果当前指令为设置指令
    else if (CmdType == NB_CMD_TYPE_SET)
    {
        TxLen += MemCopy(TxBuff + TxLen, "=", 1, 0);
    }   
   
    // cmd param
    if (pCmdParam != NULL)
    {
        TxLen += MemCopy(TxBuff + TxLen, pCmdParam, CmdParamLen, 0);
    }
    // cmd tail指令尾部添加<CR><LF> 
    TxLen += MemCopy(TxBuff + TxLen, "\r\n", 2, 0);
	nbiot_info_Struct  l_nbiot_info;		
	
	l_nbiot_info.DataLen = TxLen;
	l_nbiot_info.timeOutTicks =TimeoutTicks;
	l_nbiot_info.repeatTime = RepeatTime;
	
	for(i=0;i<4;i++)
	{
       if(xQueueSend(nbiot_Queue_info, ( uint8_t *)(&l_nbiot_info)+i, ( TickType_t ) 0 )!=pdPASS)
 	   {
          return 0;	   
	   }
	}
	
	for(i=0;i<TxLen;i++)
	{     
        xQueueSend(nbiot_Queue_data_tx, ( void *)(TxBuff+i), ( TickType_t ) 0 );	
	}
		
	return  (uint8_t)CmdIndex;
 }

//
uint8_t API_NBiot_send(void)
{
	uint8_t i =0;
	uint8_t  uxReturn=0;
     if((xSemaphoreTake(F_RspWaiting_BinarySemaphore,0)==pdTRUE)) //判断是否是发送指令
	 {
	    if(uxQueueMessagesWaiting(nbiot_Queue_info)>=4) //判断缓存区是否有数据大于1组
		{		
			for(i=0;i<4;i++)
			{
			   xQueueReceive( nbiot_Queue_info, ( (uint8_t *)&nbiot_info + i), ( TickType_t ) 0 ) ;			
			}
			for(i=0;i<nbiot_info.DataLen;i++)
			{
			   xQueueReceive( nbiot_Queue_data_tx, ( (uint8_t *)nbiot.pack_Buf + i), ( TickType_t ) 0 ) ;	
			}
		    for(i=0;i<nbiot_info.repeatTime;i++)
			{
			   API_LocalCOM_Write(COM3,nbiot.pack_Buf,nbiot_info.DataLen,ENABLE);		  
			   vTaskDelay(nbiot_info.timeOutTicks*100);       
			}
			if(i>=nbiot_info.repeatTime)
			{
			   uxReturn = 0xff;
			}		
		}	 
	
	 }
	 return uxReturn;
}








